<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button @click="$router.push('/import')" size="small" type="warning">导入</el-button>
          <el-button size="small" type="danger" @click="exportEmployee">导出</el-button>
          <el-button v-has="userPermission.add" @click="showDialog = true" size="small" type="primary">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" type="index" sortable="" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="头像" sortable="" width="100">
            <template v-slot="{ row }">
              <img 
                v-imgError="require('@/assets/common/head.jpg')"
                style="width: 100%; border-radius: 50%" 
                :src="row.staffPhoto"
                @click="showQRCode(row.staffPhoto)"
                alt="">
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column
            :formatter="formatEmployment"
            label="聘用形式"
            prop="formOfEmployment"
            sortable=""
          />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="" />
          <!-- <el-table-column label="账户状态" prop="" sortable="" /> -->
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template v-slot="{ row }">
              <el-button @click="$router.push(`/employees/detail/${row.id}`)" type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button @click="showAssignDialog(row.id)" type="text" size="small">角色</el-button>
              <el-button v-if="checkPermission(userPermission.remove)" @click="delEmployee(row.id)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :current-page.sync="page.page"
            :total="page.total"
            :page-size="page.size"
            @current-change="getEmployeeList"
            layout="total, prev, pager, next"
          />
        </el-row>
      </el-card>
    </div>
    <AddEmployee :visible.sync="showDialog"/>
    <AssignRole :employeeId="employeeId" :visible.sync="showAssignRole"/>
    <el-dialog
      title="二维码"
      :visible.sync="QRCodeVisible"
      width="30%">
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from "@/api/employees";
import AddEmployee from './components/AddEmployee.vue'
import AssignRole from './components/AssignRole.vue'
import EmployeesEnum from "@/constant/employees";
import PermissionEnum from '@/constant/permission'
import headImg from '@/assets/common/head.jpg'
import QRCode from 'qrcode'
import PermissionMixin from '@/mixin/permission'
export default {
  components: { AddEmployee, AssignRole },
  mixins: [PermissionMixin],
  data() {
    return {
      userPermission: PermissionEnum.user,
      headImg,
      page: {
        page: 1,
        size: 10,
        total: 0,
      },
      list: [],
      showDialog: false,
      QRCodeVisible: false,
      showAssignRole: false,
      employeeId: ''
    };
  },

  created() {
    this.getEmployeeList();
    this.checkPermission()
  },

  methods: {
    showQRCode(url) {
      if (url) {
        this.QRCodeVisible = true
        // vue 更新 dom是异步的
        this.$nextTick(() => {
          // console.log(this.$refs.myCanvas)
          QRCode.toCanvas(this.$refs.myCanvas, url)
        })
      }
    },
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList(this.page);
      this.page.total = total;
      this.list = rows;
    },
    formatEmployment(row, column, cellValue, index) {
      // cellValue 当前这一格的数据
      const item = EmployeesEnum.hireType.find((item) => item.id == cellValue);
      return item ? item.value : "未知";
    },
    async delEmployee(id) {
      try {
        await this.$confirm('您确定要删除嘛？')
        await delEmployee(id)
        this.$message.success('删除成功')
        this.getEmployeeList()
      } catch(e) {
        console.log(e)
      }
    },
    async exportEmployee() {
      const excel = await import('@/vendor/Export2Excel')
      const { rows } = await getEmployeeList({ page: 1, size: this.page.total })
      const { exportExcelMapPath } = EmployeesEnum
      const headers = Object.keys(exportExcelMapPath)
      const data = rows.map(item => {
        return headers.map(ele => {
          const val = item[exportExcelMapPath[ele]]
          if (ele === '聘用形式') {
             return EmployeesEnum.hireType.find(t => t.id == val)?.value || '未知'
          }
          return val
        })
      })
      excel.export_json_to_excel({
        header: headers, // 表头，必填
        data: data, // 表的具体数据，是个二维数组
        filename: '员工信息表', // 文件名
      })
    },
    showAssignDialog(id) {
      this.employeeId = id
      this.showAssignRole = true
    }
  },
};
</script>

<style scoped lang="less">
</style>
